<template>
  <div>
    <!-- 导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>YACH相关</el-breadcrumb-item>
      <el-breadcrumb-item>用户信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区 -->
    <el-card>
      <!-- 搜索 -->
      <el-row :gutter="30">
        <el-col :span="8">
          <el-input
            placeholder="请输入工号"
            clearable
            @clear="clearInput"
            v-model="queryInfo.workcode"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUser"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" :disabled="isBtnDisabled" @click="addUser"
            >添加账号</el-button
          >
        </el-col>
      </el-row>

      <!-- 显示区域 -->
      <pre>{{ user }}</pre>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        workcode: "",
      },
      user: {},
    };
  },
  created() {
    this.user = "";
  },
  methods: {
    // 清空输入框及结果
    clearInput() {
      this.input = "";
      this.user = "";
    },
    // 获取用户信息
    getUser() {
      this.$http
        .get("http://127.0.0.1:5000/query", {
          params: this.queryInfo,
        })
        .then((response) => (this.user = response.data));
    },
    // 添加用户
    addUser() {
      this.$http
        .get("http://127.0.0.1:5000/create", {
          params: this.queryInfo,
        })
        .then((response) => (this.user = response.data));
    },
  },
  computed: {
    // 按钮禁用状态
    isBtnDisabled() {
      if (this.user === "") {
        return true;
      } else if (this.user.code !== 201) {
        return true;
      } else {
        return false;
      }
    },
    //
  },
};
</script>

<style lang="less" scoped>
</style>{}